use CreativeSystems;
Go

ALTER PROC pr_GetTransactionSums
(
	@StartDateRange DATETIME,
	@EndDateRange DATETIME,
	@ClientName VARCHAR(MAX),
	@PaymentType VARCHAR(MAX)
)
AS
	SELECT COUNT(*) "Count", PaymentType, Convert(DECIMAL(10,2),SUM(Amount)) "SumAmount"
	FROM Payment p
	INNER JOIN Invoice i ON i.InvoiceID = p.InvoiceID
	INNER JOIN [Event] e ON e.EventID = i.EventID
	INNER JOIN Client c ON c.ClientID = e.ClientID
	WHERE [DateTime] >= @StartDateRange
	AND [DateTime] <= @EndDateRange
	AND c.FirstName + ' ' + c.LastName = COALESCE(@ClientName, c.FirstName + ' ' + c.LastName)
	AND PaymentType = COALESCE(@PaymentType, PaymentType)
	Group By PaymentType
	Order By PaymentType;